<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '240px'" class="sidebar">
      <div class="logo-container">
        <img src="/favicon.svg" alt="Logo" class="logo">
        <span v-if="!isCollapse" class="logo-text">管理平台</span>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        router
        class="sidebar-menu"
      >
        <template v-for="route in menuRoutes" :key="route.path">
          <!-- 二级菜单 -->
          <el-sub-menu v-if="route.children" :index="route.path">
            <template #title>
              <el-icon><component :is="route.meta?.icon" /></el-icon>
              <span>{{ route.meta?.title }}</span>
            </template>
            <el-menu-item 
              v-for="child in route.children" 
              :key="child.path"
              :index="child.path"
            >
              <el-icon><component :is="child.meta?.icon" /></el-icon>
              <span>{{ child.meta?.title }}</span>
            </el-menu-item>
          </el-sub-menu>
          
          <!-- 一级菜单 -->
          <el-menu-item v-else :index="route.path">
            <el-icon><component :is="route.meta?.icon" /></el-icon>
            <span>{{ route.meta?.title }}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>
    
    <!-- 主内容区 -->
    <el-container>
      <!-- 顶部导航 -->
      <el-header class="header">
        <div class="header-left">
          <el-button 
            :icon="Fold" 
            text 
            @click="toggleSidebar"
            class="sidebar-toggle"
          />
          <span class="page-title">个人作品集管理平台</span>
        </div>
        
        <div class="header-right">
          <el-dropdown>
            <div class="user-info">
              <el-avatar :size="32" class="user-avatar">
                <el-icon><User /></el-icon>
              </el-avatar>
              <span class="username">管理员</span>
            </div>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 页面内容 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { 
  Fold, 
  User,
  Odometer,
  FolderOpened,
  Star,
  Briefcase,
  School,
  Message,
  Setting,
  Document,
  Monitor
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

const isCollapse = ref(false)
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

const activeMenu = computed(() => {
  // 如果当前路由是子路由，返回子路由路径以激活子菜单
  return route.path
})

const menuRoutes = ref([
  {
    path: '/dashboard',
    meta: { title: '仪表盘', icon: 'Odometer' }
  },
  {
    path: '/profile', 
    meta: { title: '个人资料', icon: 'User' }
  },
  {
    path: '/projects',
    meta: { title: '项目管理', icon: 'FolderOpened' }
  },
  {
    path: '/skills',
    meta: { title: '技能管理', icon: 'Star' }
  },
  {
    path: '/experience',
    meta: { title: '经历管理', icon: 'Briefcase' }
  },
  {
    path: '/education',
    meta: { title: '教育管理', icon: 'School' }
  },
  {
    path: '/contact',
    meta: { title: '联系信息', icon: 'Message' }
  },
  {
    path: '/system',
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: '/system/dict-management',
        meta: { title: '字典管理', icon: 'Document' }
      },
      {
        path: '/system/settings',
        meta: { title: '系统设置', icon: 'Monitor' }
      }
    ]
  }
])
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.sidebar {
  background: #304156;
  transition: width 0.3s;
  overflow: hidden;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  background: #2b3441;
}

.logo {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.logo-text {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
}

.sidebar-menu {
  border: none;
  background: #304156;
}

.header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.sidebar-toggle {
  margin-right: 16px;
  font-size: 18px;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background 0.3s;
}

.user-info:hover {
  background: #f5f7fa;
}

.user-avatar {
  margin-right: 8px;
}

.username {
  font-size: 14px;
  color: #606266;
}

.main-content {
  background: #f5f7fa;
  padding: 20px;
}

:deep(.el-menu-item) {
  color: #bfcbd9;
}

:deep(.el-menu-item:hover) {
  background-color: #263445;
  color: #fff;
}

:deep(.el-menu-item.is-active) {
  background-color: #409eff;
  color: #fff;
}

:deep(.el-menu-item .el-icon) {
  margin-right: 12px;
}

:deep(.el-sub-menu__title) {
  color: #bfcbd9;
}

:deep(.el-sub-menu__title:hover) {
  background-color: #263445;
  color: #fff;
}

:deep(.el-sub-menu .el-menu-item) {
  padding-left: 50px !important;
  min-height: 40px;
  background-color: #304156;
  color: #bfcbd9;
}

:deep(.el-sub-menu .el-menu-item:hover) {
  background-color: #263445;
  color: #fff;
}

:deep(.el-sub-menu .el-menu-item.is-active) {
  background-color: #409eff;
  color: #fff;
}

:deep(.el-menu-item.is-active) {
  background-color: #409eff;
  color: #fff;
}

:deep(.el-sub-menu.is-active .el-sub-menu__title) {
  background-color: #409eff;
  color: #fff;
}

:deep(.el-menu--collapse .el-sub-menu .el-menu-item) {
  padding-left: 20px !important;
}
</style>